﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Aim2.Weapon.Projectile;
using Aim2.Ennemy;

namespace Aim2.Music {
    enum AIMCode {
        LiveElement,
        ChangeColor,
        NOP,
        ChangeBPM,
        ChangeRotation,
        ChangeBarFormula,
        ChangeTilesSpeed,
        ChangeBarsSize,
        ChangeBarsNumber,
        ChangeTilesSizeX,
        ChangeTilesSizeY,
        ChangeBarSizeFormula
    }
    class Translator {
        const string NOP = "NOP";
        const string ChangeColor = "CC"; //CC-0-100-40-30-5
        const string ChangeRotation = "Rotation"; //Rotation-1
        const string ChangeBarFormula = "BarFormula"; //BarFormula-0
        const string ChangeTilesSpeed = "TilesSpeed"; //TilesSpeed-1,5-4
        const string ChangeBarsSize = "BarsSize"; //BarSize-5,3-2
        const string ChangeTilesSizeX = "TilesSizeX"; //TilesSizeX-4,2-4
        const string ChangeTilesSizeY= "TilesSizeY"; //TilesSizeY-4,2-4
        const string ChangeBarsNumber = "BarsNumber"; //BarsNumber-4
        const string ChangeBarSizeFormula = "BarSizeFormula"; //BarSizeFormula-0
        const string ChangeBPM = "NewBPM"; //NewBPM-45,42499925

        public static AIMCode Analyze(string code) {
            if (code == NOP)
                return AIMCode.NOP;

            if (code.StartsWith(ChangeColor))
                return AIMCode.ChangeColor;

            if (code.StartsWith(ChangeBPM))
                return AIMCode.ChangeBPM;

            if (code.StartsWith(ChangeRotation))
                return AIMCode.ChangeRotation;

            if (code.StartsWith(ChangeBarFormula))
                return AIMCode.ChangeBarFormula;

            if (code.StartsWith(ChangeTilesSpeed))
                return AIMCode.ChangeTilesSpeed;

            if (code.StartsWith(ChangeTilesSizeX))
                return AIMCode.ChangeTilesSizeX;

            if (code.StartsWith(ChangeTilesSizeY))
                return AIMCode.ChangeTilesSizeY;

            if (code.StartsWith(ChangeBarsSize))
                return AIMCode.ChangeBarsSize;

            if (code.StartsWith(ChangeBarsNumber))
                return AIMCode.ChangeBarsNumber;

            if (code.StartsWith(ChangeBarSizeFormula))
                return AIMCode.ChangeBarSizeFormula;
            return AIMCode.LiveElement;
        }

        public static Tuple<bool, Color, int> GetColorChange(string code) {
            string[] args = code.Split('-');
            Color newColor = new Color(Convert.ToByte(args[2]), Convert.ToByte(args[3]), Convert.ToByte(args[4]));
            return new Tuple<bool, Color, int>(args[1] == "1", newColor, Convert.ToInt32(args[5]));
        }

        public static bool GetBool(string code) {
            return code.Split('-')[1][0] == '1';
        }

        public static int GetInt(string code) {
            return Convert.ToInt32(code.Split('-')[1]);
        }

        public static float GetFloat(string code) {
            var ret = Convert.ToDouble(code.Split('-')[1]);
            return (float)ret;
        }

        public static int GetBeats(string code) {
            return Convert.ToInt32(code.Split('-')[2]);
        }

        public static AnimatedObject TranslateElement(string element, IObjectContainer container) {
            AnimatedObject ret;
            switch (element[0]) {
                case 'A':
                    ret = new Asteroid(new Vector2(Game.Random.Next(0, 1024), 0), container);
                    break;
                case 'F':
                    ret = new Fireball(new Vector2(0,1), new Vector2((Convert.ToInt32(element[1].ToString()) + 1)*200, 0), Side.Ennemy, container);
                    break;
                case 'H':
                    float sinAmplitude;
                    float speed;
                    string[] args = element.Split('-');
                    sinAmplitude = (float) Convert.ToDouble(args[1].ToString());
                    speed = (float) Convert.ToDouble(args[2].ToString());
                    ret = new Hexagon(new Vector2((Convert.ToInt32(element[1].ToString()) + 1) * 200, 0), sinAmplitude, speed, container);
                    break;
                default:
                    throw new Exception();
            }
            return ret;
        }
    }
}
